﻿using AutoMapper;
using BS.Domain.Slaughter;
using BS.Infra;
using BS.Infra.Interface;
using MediatR;

namespace BS.Write.Api.Handler.SlaughterEnter
{
    public class AddSlaughterEnterpriseCommandHandler:IRequestHandler<AddSlaughterEnterpriseCommand, ApiResult>
    {
        private readonly ISlaughterEnterpriseRepository _slaughterEnterpriseRepository;

        private readonly IMapper _mapper;

        private readonly ILogger<AddSlaughterEnterpriseCommandHandler> _logger;

        public AddSlaughterEnterpriseCommandHandler(ISlaughterEnterpriseRepository slaughterEnterpriseRepository, IMapper mapper, ILogger<AddSlaughterEnterpriseCommandHandler> logger)
        {
            _slaughterEnterpriseRepository = slaughterEnterpriseRepository;
            _mapper = mapper;
            _logger = logger;
        }
        public async Task<ApiResult> Handle(AddSlaughterEnterpriseCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var enterprise = _mapper.Map<SlaughterEnterprise>(request);
                var result= await _slaughterEnterpriseRepository.InsertAsync(enterprise);
                return ApiResult.Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "AddSlaughterEnterpriseCommandHandler");
                return ApiResult.Fail(ex.Message);
            }
        }
    }
}
public class AddSlaughterEnterpriseCommand:IRequest<ApiResult>
{
    /// <summary>
    /// 公司名称
    /// </summary>
    public string CompanyName { get; set; } = string.Empty;
    /// <summary>
    /// 成立日期
    /// </summary>
    public DateTime EstablishDate { get; set; }
    /// <summary>
    /// 公司地址（三级联动）
    /// </summary>
    public int AddressId { get; set; }
    /// <summary>
    /// 公司简介
    /// </summary>
    public string CompanyProfile { get; set; } = string.Empty;
}